﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public partial class XQAndXY : SecurityPage
    {
        public override string PrivCode()
        {
            return "48a83b213d8454aa";
        }

        private void TVBangding()
        {
            this.TV_Student.Nodes.Clear();
            List<xqdm> xqdmlist = xqdm.All().ToList();
            List<xydm> xylist = xydm.All().ToList();
            List<DataAssociate> associatelist = DataAssociate.Where("Typeid=@0", Convert.ToInt32(EnumData.AssociateType.校区与学院)).ToList();
            xqdmlist.ForEach(p =>
            {
                TreeNode node = new TreeNode();
                node.Text = p.XQMC;
                node.Value = p.XQDM;
                node.Target = Convert.ToInt32(EnumData.TvNodeType.校区).ToString();
                node.ShowCheckBox = true;

                var s = from B in associatelist where B.ParentNo == p.XQDM select B.ChildNo;
                var q = from A in xylist
                        where s.Contains(A.XYDM)
                        select A;
                List<xydm> childlist = q.ToList();
                childlist.ForEach(o => {
                    TreeNode childNode = new TreeNode();
                    childNode.Text = o.XYMC;
                    childNode.Value = o.XYDM;
                    childNode.Target = Convert.ToInt32(EnumData.TvNodeType.学院).ToString();
                    childNode.ShowCheckBox = true;
                    node.ChildNodes.Add(childNode);
                });

                TV_Student.Nodes.Add(node);
            });
        }

        private void RepBanding()
        {
            var q=from A in LinqUtility.LinqContext.xydm
                  where !(from B in LinqUtility.LinqContext.DataAssociate 
                          where B.Typeid==Convert.ToInt32(EnumData.AssociateType.校区与学院) 
                          select B.ChildNo).Contains(A.XYDM) select A;

            int RowCount = q.Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<xydm> list = q.Skip(SkipCount).Take(PageSize).ToList();
            this.Rep_DataList.DataSource = list;
            this.Rep_DataList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            RepBanding();
            TVBangding();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepBanding();
        }

        protected void TV_Student_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.TV_Student.SelectedNode == null || this.TV_Student.SelectedNode.Target != Convert.ToInt32(EnumData.TvNodeType.校区).ToString())
                return;

            this.Lab_xq.Text = this.TV_Student.SelectedNode.Text;
            this.HidSelectId.Value = this.TV_Student.SelectedNode.Value;
        }

        protected void Btn_SetAnd_Click(object sender, EventArgs e)
        {
            string ParentNo = this.HidSelectId.Value;
            string ChildNoAll = this.hidSelectItem.Value.TrimEnd(',');

            foreach (string childNo in ChildNoAll.Split(','))
            {
                DataAssociate md = new DataAssociate();
                md.ParentNo = ParentNo;
                md.ChildNo = childNo;
                md.Typeid = Convert.ToInt32(EnumData.AssociateType.校区与学院);
                DataAssociate.Insert(md);
            }

            RepBanding();
            TVBangding();
        }

        protected void Btn_ClearAnd_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in this.TV_Student.CheckedNodes)
            {
                if (node.Target == Convert.ToInt32(EnumData.TvNodeType.学院).ToString())
                {
                    DataAssociate.Delete("ChildNo=@0 and TypeId=@1", node.Value, Convert.ToInt32(EnumData.AssociateType.校区与学院));
                }
            }
            RepBanding();
            TVBangding();
        }
    }
}